﻿using GameFramework.DataTable;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TowerDefence_Zombie.Data;
using UnityEngine;

namespace TowerDefence_Zombie
{
    public class HeroUpgrade
    {
        private DRShoppingTable m_ShoppingTable;

        public int Id
        {
            get
            {
                return m_ShoppingTable.Id;
            }
        }

        public  int UpgradeType
        {
            get
            {
                return m_ShoppingTable.UpgradeType;
            }
        }
        public int Level
        {
            get
            {
                return m_ShoppingTable.Level;
            }
        }
        public int SpecialMoney
        {
            get
            {
                return m_ShoppingTable.SpecialMoney;
            }
        }

        public int CommonMoney
        {
            get
            {
                return m_ShoppingTable.CommonMoney;
            }
        }

        public HeroUpgrade(DRShoppingTable shoppingTable)
        {
            this.m_ShoppingTable = shoppingTable;
        }
    }


    public sealed class HeroUpgradeData : DataBase
    {
        private IDataTable<DRShoppingTable> dtShoppingTable;

        private IDataTable<DRTowerPointLevelData> dtTowerPointLevelData;

        //建造器升级数据
        private Dictionary<int, HeroUpgrade> Builder_Upgrades;

        //炮塔位置升级数据
        private Dictionary<int, HeroUpgrade> TowerPosition_Upgrades;

        private Dictionary<int, int> TowerPointLevelDatas;

        //冰人炮塔升级数据
        private Dictionary<int, HeroUpgrade> IceTower_Upgrades;

        //机枪手升级数据
        private Dictionary<int, HeroUpgrade> Pistol_Upgrades;

        //老白升级数据
        private Dictionary<int, HeroUpgrade> OldMan_Upgrades;

        //钢铁侠升级数据
        private Dictionary<int, HeroUpgrade> IronMan_Upgrades;
        protected override void OnInit()
        {

        }


        protected override void OnPreload()
        {
            LoadDataTable("ShoppingTable");
            LoadDataTable("TowerPointLevelData");
        }

        protected override void OnLoad()
        {
            dtShoppingTable = GameEntry.DataTable.GetDataTable<DRShoppingTable>();
            if (dtShoppingTable == null)
                throw new System.Exception("Can not get data table DRShoppingTable");

            dtTowerPointLevelData = GameEntry.DataTable.GetDataTable<DRTowerPointLevelData>();
            if (dtTowerPointLevelData == null)
                throw new System.Exception("Can not get data table DRTowerPointLevelData");

            Builder_Upgrades = new();
            TowerPosition_Upgrades = new();
            IceTower_Upgrades = new();
            Pistol_Upgrades = new();
            OldMan_Upgrades = new();
            IronMan_Upgrades = new();
            TowerPointLevelDatas= new();
            DRShoppingTable[] dRMoneyEntitys = dtShoppingTable.GetAllDataRows();

            foreach (DRShoppingTable dRMoneyEntity in dRMoneyEntitys)
            {
                HeroUpgrade heroUpgrade = new HeroUpgrade(dRMoneyEntity);
                if (dRMoneyEntity.UpgradeType == 001)
                    Builder_Upgrades.Add(heroUpgrade.Level, heroUpgrade);
                else if (dRMoneyEntity.UpgradeType == 002)
                    TowerPosition_Upgrades.Add(heroUpgrade.Level, heroUpgrade);
                else if (dRMoneyEntity.UpgradeType == 003)
                    Pistol_Upgrades.Add(heroUpgrade.Level, heroUpgrade);
                else if (dRMoneyEntity.UpgradeType == 004)
                    IceTower_Upgrades.Add(heroUpgrade.Level, heroUpgrade);
                else if (dRMoneyEntity.UpgradeType == 005)
                    OldMan_Upgrades.Add(heroUpgrade.Level, heroUpgrade);
                else if (dRMoneyEntity.UpgradeType == 006)
                    IronMan_Upgrades.Add(heroUpgrade.Level, heroUpgrade);
            }

            DRTowerPointLevelData[] dRTowerPointLevelDatas = dtTowerPointLevelData.GetAllDataRows();
            foreach (DRTowerPointLevelData dRTowerPointLevelData in dRTowerPointLevelDatas)
                TowerPointLevelDatas[dRTowerPointLevelData.Id] = dRTowerPointLevelData.UnlockPoint;
        }

        public void GetBuilderUpgradeMaterials(int level, out int special, out int common)
        {
            special = 0;
            common = 0;
            if (Builder_Upgrades.TryGetValue(level, out HeroUpgrade heroUpgrade))
            {
                special = heroUpgrade.SpecialMoney;
                common = heroUpgrade.CommonMoney;
            }
        }

        public int GetBuilderMaxLevel()
        {
            return Builder_Upgrades.Count + 1;
        }

        public bool TryGetBuilderIsCanUpgrade()
        {
            var dataPlayer = GameEntry.Data.GetData<DataPlayer>();
            if (dataPlayer.GetBuilderLevel() < GetBuilderMaxLevel())
            {
                GetBuilderUpgradeMaterials(dataPlayer.GetBuilderLevel() + 1, out int special, out int common);
                if (dataPlayer.GetCurrency(103) >= common && dataPlayer.GetBuildSpecialMaterials() >= special)
                    return true;
            }
            return false;
        }

        public void GetTowerPositionUpgradeMaterials(int level, out int special, out int common)
        {
            special = 0;
            common = 0;
            if (TowerPosition_Upgrades.TryGetValue(level, out HeroUpgrade heroUpgrade))
            {
                special = heroUpgrade.SpecialMoney;
                common = heroUpgrade.CommonMoney;
            }
        }

        public int GetTowerPositionMaxLevel()
        {
            return TowerPosition_Upgrades.Count + 1;
        }

        public bool TryGetTowerPositionIsCanUpgrade()
        {
            var dataPlayer = GameEntry.Data.GetData<DataPlayer>();
            if (dataPlayer.GetTowerPositionLevel() < GetTowerPositionMaxLevel())
            {
                GetTowerPositionUpgradeMaterials(dataPlayer.GetTowerPositionLevel() + 1, out int special, out int common);
                if (dataPlayer.GetCurrency(103) >= common && dataPlayer.GetTowerPositionMaterials() >= special)
                    return true;
            }
            return false;
        }

        public void GetPistolUpgradeMaterials(int level, out int special, out int common)
        {
            special = 0;
            common = 0;
            if (Pistol_Upgrades.TryGetValue(level, out HeroUpgrade heroUpgrade))
            {
                special = heroUpgrade.SpecialMoney;
                common = heroUpgrade.CommonMoney;
            }
        }

        public int GetPistolMaxLevel()
        {
            return Pistol_Upgrades.Count + 1;
        }

        public bool TryGetPistolIsCanUpgrade()
        {
            var dataPlayer = GameEntry.Data.GetData<DataPlayer>();
            if (dataPlayer.GetStarSoldierLevel() < GetPistolMaxLevel())
            {
                GetPistolUpgradeMaterials(dataPlayer.GetStarSoldierLevel() + 1, out int special, out int common);
                if (dataPlayer.GetCurrency(103) >= common && dataPlayer.GetStarSoldierTowerMaterials() >= special)
                    return true;
            }
            return false;
        }



        public void GetIceTowerUpgradeMaterials(int level, out int special, out int common)
        {
            special = 0;
            common = 0;
            if (IceTower_Upgrades.TryGetValue(level, out HeroUpgrade heroUpgrade))
            {
                special = heroUpgrade.SpecialMoney;
                common = heroUpgrade.CommonMoney;
            }
        }

        public int GetIceTowerMaxLevel()
        {
            return IceTower_Upgrades.Count + 1;
        }

        public bool TryGetIceTowerIsCanUpgrade()
        {
            var dataPlayer = GameEntry.Data.GetData<DataPlayer>();
            if (dataPlayer.GetIceLevel() < GetIceTowerMaxLevel())
            {
                GetIceTowerUpgradeMaterials(dataPlayer.GetIceLevel() + 1, out int special, out int common);
                if (dataPlayer.GetCurrency(103) >= common && dataPlayer.GetIceTowerMaterials() >= special)
                    return true;
            }
            return false;
        }

        public void GetOldManUpgradeMaterials(int level, out int special, out int common)
        {
            special = 0;
            common = 0;
            if (OldMan_Upgrades.TryGetValue(level, out HeroUpgrade heroUpgrade))
            {
                special = heroUpgrade.SpecialMoney;
                common = heroUpgrade.CommonMoney;
            }
        }

        public int GetOldManMaxLevel()
        {
            return OldMan_Upgrades.Count + 1;
        }

        public bool TryGetOldManIsCanUpgrade()
        {
            var dataPlayer = GameEntry.Data.GetData<DataPlayer>();
            if (dataPlayer.GetOldManLevel() < GetOldManMaxLevel())
            {
                GetOldManUpgradeMaterials(dataPlayer.GetOldManLevel() + 1, out int special, out int common);
                if (dataPlayer.GetCurrency(103) >= common && dataPlayer.GetOldManTowerMaterials() >= special)
                    return true;
            }
            return false;
        }

        public void GetIronManUpgradeMaterials(int level, out int special, out int common)
        {
            special = 0;
            common = 0;
            if (IronMan_Upgrades.TryGetValue(level, out HeroUpgrade heroUpgrade))
            {
                special = heroUpgrade.SpecialMoney;
                common = heroUpgrade.CommonMoney;
            }
        }

        public int GetIronManMaxLevel()
        {
            return IronMan_Upgrades.Count + 1;
        }

        public bool TryGetIronManIsCanUpgrade()
        {
            var dataPlayer = GameEntry.Data.GetData<DataPlayer>();
            if (dataPlayer.GetIronManLevel() < GetIronManMaxLevel())
            {
                GetIronManUpgradeMaterials(dataPlayer.GetIronManLevel() + 1, out int special, out int common);
                if (dataPlayer.GetCurrency(103) >= common && dataPlayer.GetIronManTowerMaterials() >= special)
                    return true;
            }
            return false;
        }

        public int GetTowerPointLevelUnlockPoint(int id)
        {
            if (TowerPointLevelDatas.TryGetValue(id, out int unlockPoint))
            {
                return unlockPoint;
            }
            else
            {
                Debug.LogError("Can not find tower point level data id:" + id);
                return 101;
            }
        }

        protected override void OnUnload()
        {
            GameEntry.DataTable.DestroyDataTable<DRShoppingTable>();
            dtShoppingTable = null;
        }

        protected override void OnShutdown()
        {
        }
    }
}
